package visiopuce.DAO;

import java.math.BigDecimal;
import java.util.ArrayList;

import org.hibernate.Query;

import visiopuce.HibernateUtil;
import visiopuce.objets.Alteration;
import visiopuce.objets.CNPc;

public class CNPcDAOImpl extends GenericDAOImpl<CNPc, BigDecimal> implements CNPcDAO {

	/** Constructeur priv� */
	private CNPcDAOImpl() {
	}

	/** Instance unique non pr�initialis�e */
	private static CNPcDAOImpl INSTANCE = null;

	/** Point d'acc�s pour l'instance unique du singleton */
	public static CNPcDAO getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new CNPcDAOImpl();
		}
		return INSTANCE;
	}

	@Override
	public ArrayList<CNPc> getCNPcByBorne(Alteration alteration, long tailleChr, int seuil) {
		Long debut = Math.max(0, alteration.getDebutHg19() - seuil);
		Long fin = Math.min(alteration.getFinHg19() + seuil, tailleChr);
		ArrayList<CNPc> cnpcs = null;
		String sql = null;
		Query query;
		sql = "SELECT g FROM CNPc g WHERE g.debutHg19 IS NOT NULL AND g.debutHg19 <= :fin AND g.finHg19 IS NOT NULL AND g.finHg19 >= :debut AND g.chromosome LIKE :chromosome ORDER BY g.debutHg19";
		query = HibernateUtil.getSession().createQuery(sql).setParameter("debut", debut);
		query.setParameter("fin", fin);
		query.setParameter("chromosome", alteration.getChromosome());
		cnpcs = (ArrayList<CNPc>) query.list();
		return cnpcs;
	}
}